package com.controller;

import com.alibaba.fastjson.JSON;
import com.pojo.SysRole;
import com.service.SysRoleService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
    private static final Logger logger=Logger.getLogger(SysRoleController.class);
    @Resource
    private SysRoleService roleService;
    @RequestMapping("/list")
    public String getSysRoleList(Model model){
        List<SysRole> sysRoleList=null;
        try {
            sysRoleList=roleService.roleList();
            model.addAttribute("sysRoleList",sysRoleList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("角色列表接口访问失败!");
            return "redirect:/sysRole/toSysError";
        }
        return "sysRole/list";
    }
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }

    @ResponseBody
    @RequestMapping( value = "/simpleList",produces = {"application/json;charset=UTF-8"})
    public String simpleList(Model model){
        List<SysRole> roleList=null;
        String pwdJSON=null;
        try {
            roleList=roleService.simpleList();
            model.addAttribute("roleList",roleList);
        }catch (Exception e){
            e.printStackTrace();
            return "redirect:/sysRole/toSysError";
        }
        return JSON.toJSONString(roleList);
    }

    @ResponseBody
    @RequestMapping("/del/{id}")
    public String del(@PathVariable String id){
        if(roleService.del(id)){
            return JSON.toJSONString(true);
        }else{
            return JSON.toJSONString(false);
        }
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "sysRole/add";
    }
    @RequestMapping("/add")
    public String add(SysRole sysRole){
        if(roleService.add(sysRole)){
            return "redirect:/sysRole/list";
        }
        return "/sysRole/add";
    }
    @ResponseBody
    @RequestMapping("/codeExist/{code}")
    public String codeExist(@PathVariable String code){
        int codeExist=-1;
        codeExist=roleService.codeExist(code);
        return JSON.toJSONString(codeExist);
    }
}
